﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HomeFramework.Model;

namespace HomeFramework
{
    public partial class _Default : System.Web.UI.Page
    {
        readonly TransactionScript ts = new TransactionScript();

        protected void Page_Load(object sender, EventArgs e)
        {
            MessageBox.Visible = false;
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            GridView1.DataSource = ts.GetHandlers();
            GridView1.DataBind();
        }

        protected void uploadButton_Click(object sender, EventArgs e)
        {
            if (fileUpload.Visible == false)
                fileUpload.Visible = true;
            else if (Request.Files.Count == 1)
            {
                try
                {
                    var input = Request.Files[0].InputStream;
                    byte[] b = new byte[input.Length];
                    input.Read(b, 0, (int)input.Length);

                    foreach (var h in Handler.Utility.ReadAssembly(b))
                        ts.AddHandler(h);

                    fileUpload.Visible = false;
                }
                catch (Exception ex)
                {
                    MessageBox.Visible = true;
                    messageLabel.Text = ex.Message;
                }
            }
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            ts.RemoveHandlerByID((int)e.Keys[0]);
        }
    }
}
